package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国娱乐网
 * @author Persh
 *
 */
public class _67NewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://[a-z]*.67.com/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.html",
		"http://.*.67.com/.*/[0-9]*/[0-9]*/[0-9]*/[0-9]*.html",
		"http://bbs.67.com/threadslide-[0-9]*-1-1.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select(".artSide").remove();
		doc.select(".trace").remove();
		doc.select(".hd_wumii").remove();
		doc.select(".wumii-hook").remove();
		doc.select("#nextContent").remove();

		title =doc.select("div.artCon h1").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "-");
			if(StringUtils.isBlank(title)){
			}
		}
		
		doc.select("div.rdtj").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#pageContent"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#photoText"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#imgCon"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#imgSummury p"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#divContent"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".postmessage"));
		}

		author=JSoupUtils.matchAuthor(doc, "来源：","ID：");

		date=DateUtils.matchDate(doc.select("div.artInfo").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("span.fr").text());
		}
		if(date == null){
			date=JSoupUtils.matchDate(doc, "发表于：");
		}
		if(date == null){
			date=DateUtils.matchDate(url);
		}


		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		_67NewsAnalyse analyse=new _67NewsAnalyse();
		String url="http://news.67.com/xianchang/2015/12/06/820626.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.isDetailPage(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
